package com.uenpay.im.ui.old.activity.personal;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.qyx.android.database.TalkMsgManager;
import com.qyx.android.database.TopListMsgManager;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.PushServiceConn;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.ui.old.activity.pwd.SetPayPwdActivity;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.weight.BottomDialog2;
import com.uenpay.im.weight.DialogUtility;
import com.uenpay.util.app.ActivityStack;
import com.uenpay.util.app.base.BaseActivity;

/**
 * 设置
 *
 * @author SL
 * @copyright http://www.qiyunxin.com
 */
public class SettingActivity extends BaseActivity implements OnClickListener {

    private ImageView voice_btn, shake_btn, new_msg_notic_btn;
    private int voice = 0;
    private int shake = 0;
    private int new_msg_notice = 0;
    private View new_msg_voice, voice_layout, voice_view, shake_layout;
    private TopListMsgManager topListMsgManager = new TopListMsgManager();
    private TalkMsgManager takeMsgManager = new TalkMsgManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_setting_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        initData();
    }

    protected void initView() {

        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.setting));
        voice_btn = (ImageView) findViewById(R.id.voice_btn);
        shake_btn = (ImageView) findViewById(R.id.shake_btn);
        new_msg_notic_btn = (ImageView) findViewById(R.id.new_msg_notic_btn);
        new_msg_voice = findViewById(R.id.new_msg_voice);
        voice_layout = findViewById(R.id.voice_layout);
        voice_view = findViewById(R.id.voice_view);
        shake_layout = findViewById(R.id.shake_layout);
        voice_btn.setOnClickListener(this);
        shake_btn.setOnClickListener(this);
        new_msg_notic_btn.setOnClickListener(this);
    }

    protected void initListener() {
        findViewById(R.id.setting_update_pwd_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        ActivityHandle.startActivity(SettingActivity.this,
                                UpdatePwdActivity.class, null, -1);
                    }
                });

        findViewById(R.id.setting_update_pay_pwd_layout).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(SettingActivity.this,
                        SetPayPwdActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.setting_clear_msg_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        clearHistory();
                    }
                });
        findViewById(R.id.setting_exit_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        showExitDialog();
                    }
                });

    }

    private void initData() {
        voice = IMApplication.config.getMsgVoice();
        shake = IMApplication.config.getMsgShake();
        new_msg_notice = IMApplication.config.getMsgNotice();
        if (voice == 1) {
            voice_btn.setImageResource(R.drawable.img_button_toggle_on);
        } else {
            voice_btn.setImageResource(R.drawable.img_button_toggle_off);
        }
        if (shake == 1) {
            shake_btn.setImageResource(R.drawable.img_button_toggle_on);
        } else {
            shake_btn.setImageResource(R.drawable.img_button_toggle_off);
        }
        if (new_msg_notice == 1) {
            new_msg_notic_btn.setImageResource(R.drawable.img_button_toggle_on);
        } else {
            new_msg_notic_btn
                    .setImageResource(R.drawable.img_button_toggle_off);
            new_msg_voice.setVisibility(View.GONE);
            voice_layout.setVisibility(View.GONE);
            voice_view.setVisibility(View.GONE);
            shake_layout.setVisibility(View.GONE);
        }
    }


    private void showExitDialog() {
        BottomDialog2.showBottomDialog(this,
                getResources().getString(R.string.exit_app_dialog), "", "", -1,
                new IOnBottomDialogListener() {

                    @Override
                    public void onClicked() {
                        exitApp();
                    }
                });
    }

    // 退出APP
    private void exitApp() {
        PushServiceConn.getInstance(SettingActivity.this).stopChatService();
        ARouter.getInstance().build("/main/login").navigation(this);
        IMApplication.notification_sum = 0;
        NotificationManager mNotificationManager = (NotificationManager) IMApplication
                .getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(0);
        IMApplication.IS_GET_ALL_FRIENDS = true;
//        IMApplication.getInstance().ClearAllActivity();
        ActivityStack.getInstance().popAll();
    }

    /**
     * 清除所有消息
     */
    private void clearHistory() {

        DialogUtility.showDialog(SettingActivity.this, R.string.clear_all_msg,
                R.string.clear, R.string.cancel, true,
                new IOnBottomDialogListener() {

                    @Override
                    public void onClicked() {
                        // 清除会话消息
                        takeMsgManager.clearEmpty();
                        // 清除会话列表消息
                        topListMsgManager.clearEmpty();
                        // 通知会话列表刷新数据
                        Intent intent = new Intent();
                        intent.setAction(BroadcastAction.ACTION_CLEAR_MSG_COUNT);
                        sendBroadcast(intent);
                        IMApplication.showToast(getResources().getString(
                                R.string.delete_completed));
                        finish();
                    }
                });

    }

    @Override
    protected void onDestroy() {
        IMApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }

    @Override
    public void onClick(View arg0) {
        int i = arg0.getId();
        if (i == R.id.voice_btn) {
            if (voice == 1) {
                voice = 0;
            } else {
                voice = 1;
            }

        } else if (i == R.id.shake_btn) {
            if (shake == 1) {
                shake = 0;
            } else {
                shake = 1;
            }

        } else if (i == R.id.new_msg_notic_btn) {
            if (new_msg_notice == 0) {
                new_msg_notice = 1;

            } else {
                new_msg_notice = 0;
            }

        } else {
        }
        setAllMsgPush(shake, voice, new_msg_notice);
    }

    /**
     * @param shock
     * @param beep
     * @param new_msg_notice
     */
    private void setAllMsgPush(final int shock, final int beep,
                               final int new_msg_notice) {

        if (shock == 1) {
            shake_btn.setImageResource(R.drawable.img_button_toggle_on);
        } else {
            shake_btn.setImageResource(R.drawable.img_button_toggle_off);
        }
        if (beep == 1) {
            voice_btn.setImageResource(R.drawable.img_button_toggle_on);
        } else {
            voice_btn.setImageResource(R.drawable.img_button_toggle_off);
        }

        if (new_msg_notice == 1) {
            new_msg_notic_btn.setImageResource(R.drawable.img_button_toggle_on);
            new_msg_voice.setVisibility(View.VISIBLE);
            voice_layout.setVisibility(View.VISIBLE);
            voice_view.setVisibility(View.VISIBLE);
            shake_layout.setVisibility(View.VISIBLE);
        } else {
            new_msg_notic_btn
                    .setImageResource(R.drawable.img_button_toggle_off);
            new_msg_voice.setVisibility(View.GONE);
            voice_layout.setVisibility(View.GONE);
            voice_view.setVisibility(View.GONE);
            shake_layout.setVisibility(View.GONE);
        }
        IMApplication.config.setMsgShake(shock);
        IMApplication.config.setMsgVoice(beep);
        IMApplication.config.setMsgNotic(new_msg_notice);
    }

}
